package com.lichao.cdc;


import com.lichao.pojo.*;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

// 假设你有 Event 和 UserChange 类来映射数据流
public class ProcessFunctionExample1 {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        SingleOutputStreamOperator<BehaviorFull> orderStream = env.addSource(new MysqlSource())
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy.<BehaviorFull>forMonotonousTimestamps()
                                .withTimestampAssigner(new SerializableTimestampAssigner<BehaviorFull>() {
                                    @Override
                                    public long extractTimestamp(BehaviorFull element, long recordTimestamp) {
                                        element.setClientId(element.getClientId() + element.getPageId());
                                        System.out.println("数据源1 " + element.getClientId());
                                        return element.getTimestamp();
                                    }
                                }));

        SingleOutputStreamOperator<ClickBehaviorFull> clickStream = env.addSource(new ClickSource1())
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy.<ClickBehaviorFull>forMonotonousTimestamps()
                                .withTimestampAssigner(new SerializableTimestampAssigner<ClickBehaviorFull>() {
                                    @Override
                                    public long extractTimestamp(ClickBehaviorFull element, long recordTimestamp) {
                                        element.setClientId(element.getClientId() + element.getPageId());
                                        System.out.println("数据源2 " + element.getClientId());
                                        return element.getTimestamp();
                                    }
                                }));

        orderStream.keyBy(BehaviorFull::getClientId)
                .intervalJoin(clickStream.keyBy(ClickBehaviorFull::getClientId))
                .between(Time.seconds(-5), Time.seconds(10))
                .process(new ProcessJoinFunction<BehaviorFull, ClickBehaviorFull, String>() {
                    @Override
                    public void processElement(BehaviorFull left, ClickBehaviorFull right, Context ctx, Collector<String> out) throws Exception {
                        out.collect(right + " => " + left);
                    }
                }).print();

        env.execute();
    }
}
